package com.four.live.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.four.live.dao.GoodsMapper;
import com.four.live.entity.Goods;
import com.four.live.entity.Live;
import com.four.live.dao.LiveMapper;
import com.four.live.fegin.ShopFeginService;
import com.four.live.fegin.UserFeginService;
import com.four.live.service.ILiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.four.live.vo.LiveVo;
import com.guli.common.vo.ManageVo;
import com.guli.common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-12-23
 */
@Service
public class LiveServiceImpl extends ServiceImpl<LiveMapper, Live> implements ILiveService {

    @Autowired
    private LiveMapper liveMapper;

    @Autowired
    private UserFeginService userFeginService;

    @Autowired
    private ShopFeginService shopFeginService;

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 直播列表，精准，分页，模糊
     *
     * @param vo
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<Live> liveList(LiveVo vo, Integer page, Integer size) {

        Page<Live> livePage = new Page<>(page, size);
        return liveMapper.liveList(livePage, vo);
    }

    /**
     * 直播根据id查询本条数据
     *
     * @param id
     * @return
     */
    @Override
    public Live findById(Long id) {
        return liveMapper.findById(id);
    }

    /**
     * 直播根据状态删除
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {

        liveMapper.deleteById(id);
    }

    /**
     * 直播根据直播开启的状态进行详情
     *
     * @param id
     * @return
     */
    @Override
    public Live selectList(Long id) {

        return liveMapper.selectById(id);

    }


    /**
     * 创建直播
     * @param live
     */
    @Override
    public void addLive(Live live,String loginName) {
        UserVo userVo=userFeginService.findByUname(loginName);
        live.setHostId(userVo.getUserId());
        live.setHostName(userVo.getUserName());
        liveMapper.addLive(live);
        //添加到直播商品记录表
        ManageVo manageVo=shopFeginService.getManageById(live.getGoodsId());
        Goods goods = new Goods();
        goods.setGoodsId(manageVo.getManId());
        goods.setHostId(userVo.getUserId());
        goods.setGoodsImgs(manageVo.getManIcon());
        goods.setGoodsName(manageVo.getManName());
        goods.setGoodsPrice(manageVo.getPrice());
        goods.setGoodsAttr(1);
        goods.setGoodsCategory(manageVo.getCatId());
        goods.setBuyNums(0);
        goods.setClickNums(0);
        goods.setGoodsRemain(manageVo.getStock());
        goods.setLiveId(live.getLiveId());
        goodsMapper.addGoods(goods);

    }

    /**
     * 开启直播
     * @param liveId
     */
    @Override
    public void openLive(Integer liveId) {
        //先根据直播id查询到该直播信息
        Live live=liveMapper.getLiveById(liveId);
        if(live.getLiveStatus()==0){
            //点击开启1
            int liveStatus=1;
            liveMapper.updateLiveStatus(liveStatus);
        }else if(live.getLiveStatus()==1){
            //点击关闭0
            int liveStatus=0;
            liveMapper.updateLiveStatus(liveStatus);
        }
    }
}
